package com.sanhuo.idms.service;


import com.sanhuo.idms.bo.DormHighSearchBO;
import com.sanhuo.idms.bo.DormSaveBO;
import com.sanhuo.idms.dao.DormMapper;
import com.sanhuo.idms.entity.DeviceState;
import com.sanhuo.idms.entity.Dorm;
import com.sanhuo.idms.entity.Room;
import com.sanhuo.idms.entity.Tung;
import com.sanhuo.idms.vo.DormDetailVO;
import com.sanhuo.idms.vo.DormPageVO;
import com.sanhuo.lib.ExceptionUtil;
import com.sanhuo.security.UserVo;
import com.sanhuo.triild.Basic.BaseMapper;
import com.sanhuo.triild.Basic.BaseService;
import com.sanhuo.triild.annotation.MapperWire;
import com.sanhuo.triild.page.Page;
import com.sanhuo.triild.page.PageSearch;
import com.sanhuo.utils.basic.BeanUtil;
import com.sanhuo.utils.basic.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class DormService extends BaseService<Dorm> {

    @MapperWire
    private DormMapper dormMapper;
    @Autowired
    private RoomService roomService;
    @Autowired
    private TungService tungService;


    @Override
    public BaseMapper<Dorm> getBaseMapper() {
        return dormMapper;
    }


    public Dorm findByTungAndRoom(String tungId, String roomId) {
        return dormMapper.findByTungAndRoom(tungId, roomId);
    }

    public void insert(final UserVo user, DormSaveBO bo) throws IllegalAccessException {
        if (StringUtil.isNotBlank(bo.getRoomId())) {
            Room room = roomService.findById(bo.getRoomId());
            ExceptionUtil.throwException(room == null, "不存在此房间");
        }
        if (StringUtil.isNotBlank(bo.getTungId())) {
            Tung tung = tungService.findById(bo.getTungId());
            ExceptionUtil.throwException(tung == null, "不存在此栋");
        }
        ExceptionUtil.throwException(!BeanUtil.isNull(dormMapper.findByTungAndRoom(bo.getTungId(), bo.getRoomId())), "该宿舍已存在,请不要重复添加");
        Dorm dorm = new Dorm();
        dorm.setId(StringUtil.UUID());
        BeanUtil.copyBean(bo, dorm);
        dorm.creatorAndUpdatorInfo(user);
        save(dorm);
    }

    public void insert(Dorm dorm) {
        if (StringUtil.isNotBlank(dorm.getRoomId())) {
            Room room = roomService.findById(dorm.getRoomId());
            ExceptionUtil.throwException(room == null, "不存在此房间");
        }
        if (StringUtil.isNotBlank(dorm.getTungId())) {
            Tung tung = tungService.findById(dorm.getTungId());
            ExceptionUtil.throwException(tung == null, "不存在此栋");
        }
        ExceptionUtil.throwException(!BeanUtil.isNull(dormMapper.findByTungAndRoom(dorm.getTungId(), dorm.getRoomId())), "该宿舍已存在,请不要重复添加");
        save(dorm);
    }

    public Page<DormPageVO> listAll(DormHighSearchBO bo, PageSearch pageSearch) {
        return dormMapper.listAll(bo, pageSearch);
    }

    public DormDetailVO detail(String id) {
        return dormMapper.detail(id);
    }

    public Dorm findByNum(Integer tungNum, Integer roomNum) {
        return dormMapper.findByNum(tungNum, roomNum);
    }
}
